<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Admin as AdminModel;
use app\common\model\AdminGroup as AdminGroupModel;
use app\common\model\AdminAuthrule as AdminAuthruleModel;

use app\admin\validate\Admin as AdminValidate;
use think\exception\ValidateException;

use think\facade\Request;
use think\facade\View;
use think\facade\Config;



//------------------------
// 管理员管理
//-------------------------
class Admin extends Base
{
 
	/******
	* 管理员管理
	* @return mixed
	******/
    public function index()
    {
        $admin_list = AdminModel::select();
		View::assign('admin_list',$admin_list);
		return View::fetch('index');
    }

	/******
	* 添加管理员
	* @return mixed
	******/
    public function add()
    {

		if (Request::isPost()) {

            $data	= Request::only(['avatar', 'username', 'password', 'nickname', 'admin_group_id', 'status']);

            try {
				validate(AdminValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			$data['password'] = md5(Config::get('app.password_salt') . $data['password']);
			if (AdminModel::create($data)) {
				$this->success('保存成功',url('admin/admin/index'));
			} else {
				$this->error('保存失败');
			}

        }

        $admin_group_list = AdminGroupModel::select();

		View::assign('admin_group_list',$admin_group_list);
		return View::fetch('add');
    }


	/******
	* 编辑管理员
	* @param $id
	* @return mixed
	******/
    public function edit($id)
    {

		if (Request::isPost()) {

            $data	= Request::only(['id','avatar', 'username', 'password', 'nickname', 'admin_group_id', 'status']);

            try {
				validate(AdminValidate::class)->scene('edit')->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if(!empty($data['password'])){
				$data['password'] = md5(Config::get('app.password_salt') . $data['password']);
			}else{
				unset($data['password']);
			}
			
			if (AdminModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/admin/index'));
			} else {
				$this->error('保存失败');
			}


        }


        $admin				= AdminModel::find($id);
        $auth_group_list	= AdminGroupModel::select();

		View::assign('admin',$admin);
		View::assign('admin_group_list',$auth_group_list);
		return View::fetch('edit');
    }

	/******
	* 删除管理员
	* @param $id
	******/
    public function delete($id)
    {
        if ($id == 1) {
            $this->error('默认管理员不可删除');
        }
        if (AdminModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}